package perso.android.savethemage.v1;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.svg.opengl.texture.atlas.bitmap.SVGBitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;

import android.util.Log;

public abstract class Entite {
	// ===========================================================
	// Constants
	// ===========================================================
    protected AnimatedSprite face;
    protected Body body;

	protected FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

   // protected BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlas;
	private BitmapTextureAtlas mBitmapTextureAtlasParticule;
	
    //protected ITiledTextureRegion mSVGTestTextureRegions;
	protected ITextureRegion mParticleTextureRegion;
	
    protected VertexBufferObjectManager vertexBufferObjectManager;
    
    protected TiledTextureRegion mEntiteTextureRegion;
    protected BuildableBitmapTextureAtlas mBitmapTextureAtlasEntite;
	// ===========================================================
	// Methods
	// ===========================================================
    //*
	public void onCreateResources(String nomFichierSVG, String nomParticulesPNG, int pWidth, int pHeight, int NbLigneDansSprite, int nbColonneDansSprite, GameActivity gameActivity) {
		
		this.vertexBufferObjectManager = gameActivity.getVertexBufferObjectManager();

		
		this.mBitmapTextureAtlasEntite = new BuildableBitmapTextureAtlas(gameActivity.getTextureManager(), pWidth, pHeight, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        
        this.mEntiteTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlasEntite, 
        		gameActivity.getApplicationContext(), nomFichierSVG+".png", nbColonneDansSprite, NbLigneDansSprite);

		try {
			this.mBitmapTextureAtlasEntite.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
			this.mBitmapTextureAtlasEntite.load();
		} catch (TextureAtlasBuilderException e) {
			Debug.e(e);
		}

        
        
        
        //*
		///////////////////////////////////////////////// Particule
		
		this.mBitmapTextureAtlasParticule = new BitmapTextureAtlas(gameActivity.getTextureManager(), 16, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlasParticule, 
        		gameActivity.getApplicationContext(), nomParticulesPNG+".png", 0, 0);
		this.mBitmapTextureAtlasParticule.load();
		//*/
	}
	
	public void onPopulateScene(PhysicsWorld mPhysicsWorld, Scene scene){
        scene.attachChild(face);
	}
	
	public void appli(PhysicsWorld mPhysicsWorld){
		this.face.setVisible(true);
	}
	
	public void onDepopulateScene(PhysicsWorld mPhysicsWorld, Scene scene){
		this.face.setVisible(false);
		//*
		if(this.face.getChildCount() > 0) {
			mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape((AnimatedSprite)this.face.getLastChild()).getBody().setActive(false);
	       // Body b = mPhysicsWorld.getPhysicsConnectorManager().findBodyByShape(this.face);
	       this.face.detachChild(this.face.getLastChild());
		}
		//*/
		//Log.d("Misa.Entite", "DepopulateScene");
		if( scene != null)
			scene.detachChild(this.face);
        //*/
		//mPhysicsWorld.unregisterPhysicsConnector(mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(this.face));
        //destruction ?
        //mPhysicsWorld.destroyBody(this.body);
	}
//*/
}

